<?php
namespace haibao\cms\business;

use haibao\cms\data\SphinxDb as SphinxDbData;
use haibao\cms\model\filter\SphinxDb as SphinxDbFilter;
use \haibao\cms\model\data\SphinxDb as SphinxDbModel;
use haibao\frame\data\query\Condition;

class SphinxDb extends Base{
	
	public static function getHighLightInfo($ids,$type){
		$data = new SphinxDbData();
		
		return $data->getHighLightInfo($ids,$type);
	}
	
	public static function getAll($filter){
		$data = new SphinxDbData();
		
		return $data->getAll($filter);
	}
	
	public static function searchImageList($bannerIds,$isParent,$pager){
		$ids = array();
		$data = new SphinxDbData();
		if ($isParent){
			$ids = $data->searchImageList($bannerIds,$pager);
		}else{
			$filter = new \haibao\cms\model\filter\SphinxDb();
			$filter->setPager($pager);
			$filter->where(SphinxDbFilter::CONDITION_FIELD_LEVEL,Condition::CONDITION_GREATEREQUAL,3);
			$filter->where(SphinxDbFilter::CONDITION_FIELD_STATUS,Condition::CONDITION_EQUAL,\haibao\cms\model\data\SphinxDb::STATUS_ENABLE);
			$filter->where(SphinxDbFilter::CONDITION_FIELD_TYPE,Condition::CONDITION_IN,array(
					\haibao\cms\model\data\SphinxDb::TYPE_ARTICLE,
					\haibao\cms\model\data\SphinxDb::TYPE_IMAGE
			));
			$filter->where(SphinxDbFilter::CONDITION_FIELD_TAG_IDS,Condition::CONDITION_IN,$bannerIds);
			$filter->order(SphinxDbFilter::CONDITION_FIELD_SHOW_TIME,false);
			$ids = $data->searchByFilter($filter);
			$pager = $filter->getPager();
		}
		
		return $ids;
	}
	
	public static function searchByFilter($filter){
		$data = new SphinxDbData();

		return $data->searchByFilter($filter);
	}
	
	/**
	 * 
	 * @param array $contentArr
	 * @param string $keyword
	 * @param array $options
	 * array(
					'before_match'=>'<b>',	#高亮前缀
					'after_match'=>'</b>',	#高亮后缀
					'chunk_separator'=>'...',	#区块连接符
					'limit'=>150,			#最大字数限制
					'around'=>10,			#每个关键词左右选取词的数目
					'allow_empty'=>true,	#没有匹配关键词时返回空字符串,默认true
					'html_strip_mode'=>'strip',	#是否去除html标记，默认index，不去除,
					'exact_phrase'=>false,	#是否仅高亮精确匹配的整个查询词组，而不是单独的关键词,默认false
		)
	 */
	public static function highLight($contentArr,$keyword,$options = array()){
		$data = new SphinxDbData();
		
		return $data->highLight($contentArr,$keyword,$options);
	}
	
	/**
	 * 聚合关键词查索引
	 * @param unknown_type $name
	 * @param unknown_type $currentPage
	 * @param unknown_type $pageSize
	 * @return multitype:multitype: NULL
	 */
	public static function searchPolymer($name,$currentPage,$pageSize,$showTime = null,$isAsc = true){
		$searchTypes = array(
				SphinxDbModel::TYPE_ARTICLE,
				SphinxDbModel::TYPE_FEATURE,
				SphinxDbModel::TYPE_IMAGE,
				SphinxDbModel::TYPE_VIDEO,
		);
	
		$filter = new SphinxDbFilter();
		$filter->where(
				SphinxDbFilter::CONDITION_FIELD_TITLE_TAG,
				Condition::CONDITION_EQUAL,
				$name
		);
		$filter->where(
				SphinxDbFilter::CONDITION_FIELD_TYPE,
				Condition::CONDITION_IN,
				$searchTypes
		);
		$filter->where(
				SphinxDbFilter::CONDITION_FIELD_STATUS,
				Condition::CONDITION_EQUAL,
				SphinxDbModel::STATUS_ENABLE
		);
		$filter->where(
				SphinxDbFilter::CONDITION_FIELD_IS_PC_SEARCH,
				Condition::CONDITION_EQUAL,
				SphinxDbModel::STATUS_ENABLE
		);
		if($showTime){
			$filter->where(
					SphinxDbFilter::CONDITION_FIELD_SHOW_TIME,
					Condition::CONDITION_BETWEEN,array($showTime,time())
			);
		}
		
		$filter->setPager(new \haibao\frame\data\query\Pager($pageSize,$currentPage));
		$filter->order(\haibao\cms\model\filter\SphinxDb::CONDITION_FIELD_SHOW_TIME,$isAsc);
	
		$articleData = new \haibao\cms\data\Article();
		$searchResult = $articleData->searchByKeywordFilter($filter);
		
		return array('result'=>$searchResult,'pager'=>$filter->getPager());
	}
	
	
}